<template>
  <div class="searchBox">
    <div class="search">
      <div class="searchLeft"></div>
      <div class="searchMain" @mousemove="searchListIsFocus = true" @mouseout="searchListIsFocus = false">
        <input type="text" v-model="text" @focusin="searchBoxIsFocus = true" @focusout="searchBoxIsFocus = false" @keydown.enter="search" @click="getSearchSuggestions(searchSuggestions[navIndex].replace('$k', text))" @keyup="getSearchSuggestions(searchSuggestions[navIndex].replace('$k', text))" placeholder="一键找到你想要的">
        <ul id="searchList"></ul>
      </div>
      <div class="searchBt" @click="search">搜索</div>
    </div>
    <div class="SearchType">
      <img class="SearchTypeCard" :src="item.image" :style="navIndex === index ? navBg : ''" v-for="(item,index) in SearchType" :key="index" @click="getBgcolor(index)" alt="" :title="item.name">
    </div>
  </div>
</template>

<script>
import {SearchType} from "@/config/SearchType";
import {searchSuggestions} from "@/config/SearchSuggestions";
import jsonp from 'jsonp';

export default {
  name: "SearchBox",
  data() {
    return {
      SearchType,
      navBg:"border: 2px solid white;" +
          "background-color:white;",
      text: "",
      link: SearchType[0].link,
      navIndex: 0,
      searchSuggestions,
      jsonp,
      searchListIsFocus: false,
      searchBoxIsFocus: false
    }
  },
  watch:{
    searchListIsFocus(){
      if (this.searchListIsFocus || this.searchBoxIsFocus){
        document.querySelector("#searchList").style.display = "";
      } else {
        document.querySelector("#searchList").style.display = "none";
      }
    }
  },
  methods: {
    getBgcolor(index) {
      this.navIndex = index;
      this.link = SearchType[index].link;
    },
    search() {
      if (this.text === "") return;
      window.open(this.link.replace('$k', this.text));
    },
    getSearchSuggestions(url) {
      let searchList = document.querySelector("#searchList");
      searchList.innerHTML = "";
      if (this.text === "") return;
      function jsonp(url, callback) {
        const script = document.createElement('script');
        script.src = url + '&callback=' + callback;
        document.head.appendChild(script);
      }

      function handleSearchSuggestions(data) {
        let searchList = document.querySelector("#searchList");
        searchList.innerHTML = "";
        if (data['g'] !== undefined) {
          let html = "";
          for (let item of data['g']){
            html = html + "<a href=\"https://www.baidu.com/s?ie=utf-8&wd=" + item['q'] + "\" target=\"_blank\"><li>" + item['q'] + "</li></a>"
          }
          searchList.innerHTML = html;
        }
      }

      // Generate a unique callback function name
      const callbackName = 'jsonpCallback_' + Date.now() + '_' + Math.floor(Math.random() * 1000);

      window[callbackName] = function(data) {
        delete window[callbackName];

        handleSearchSuggestions(data);
      };

      const finalJsonpUrl = url + '&callback=' + callbackName;

      jsonp(finalJsonpUrl, callbackName);
    }
  }
}
</script>

<style>
.searchBox {
  margin: auto;
  width: 80%;
  height: 230px;
}

.search {
  display: flex;
  width: 400px;
  height: 40px;
  margin: 150px auto 0 auto;
  border-radius: 15px;
  background-color: #42b983;
}

.searchLeft {
  width: 16px;
  height: 40px;
  float: left;
  border-radius: 15px 0 0 15px;
  background-color: white;
}

.search input {
  height: 40px;
  width: 310px;
  float: left;
  font-size: 16px;
  border-style: none;
  background-color: white;
}

.searchBt {
  width: 74px;
  height: 40px;
  background-color: #FFF782;
  float: left;
  text-align: center;
  line-height: 40px;
  border-radius: 0 15px 15px 0;
}

.searchBt:hover {
  cursor: pointer;
}

.SearchType {
  margin: 30px auto 0 auto;
  width: 306px;
  height: 134px;
  border-radius: 10px;
  background-color: rgba(255, 255, 255, 0.3);
  backdrop-filter: blur(10px);
}

.SearchTypeCard {
  float: left;
  margin: 8px 6px 8px 6px;
  width: 45px;
  height: 45px;
  border-radius: 20px;
  border: 2px solid white;
}

.SearchTypeCard:hover {
  cursor: pointer;
  background-color: white;
}

#searchList{
  background-color: rgba(255,255,255,0.8);
  margin-top: 40px;
  border-bottom-left-radius: 15px;
  border-bottom-right-radius: 15px;
}
#searchList > a > li{
  margin-top: 8px;
  margin-bottom: 8px;
  height: 26px;
  line-height: 26px;
  padding-left: 8px;
  overflow: hidden;
}

#searchList > a > li:hover{
  background-color: #FFF782;
}

.searchMain{
  z-index: 100;
}
</style>